﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponents.Design.Editors;
using System.Windows.Forms;
using ProgNetDxComponents.ExtenderProviders;
using ProgNetDxComponents.ExtenderProviders.Browsers;

namespace ProgNetDxComponents.Design.Editors
{
    public class BrowserCollectionEditor : NamedCollectionEditor
    {
        public BrowserCollectionEditor(Type type) : base(type) { }
        protected override bool GenerateConstantClass
        {
            get
            {
                return true;
            }
        }
       
        protected override void ValidateCollection(object collection)
        {
            if (listbox != null)
            {
                BrowserCollection coll = collection as BrowserCollection;
                if (coll != null)
                {
                    List<string> browsers = new List<string>();
                    
                    foreach(Browser value in coll)
                    {
                        if (value != null)
                        {
                            if (value.Name.IsNotNull())
                            {
                                if (browsers.Contains(value.Name))
                                    AddError("Duplicated browser " + value.Name.AddDoubleQuotes());
                                else
                                    browsers.Add(value.Name);
                            }
                            else
                            {
                                AddError("At least one browser does not have Name");
                            }
                            if (value.ValueMember.IsNull())
                            {
                                AddError("Browser " + value.Name.AddDoubleQuotes() + " does not have ValueMember");
                            }
                            else if (!value.Columns.Contains(value.ValueMember))
                            {
                                AddError("Browser " + value.Name.AddDoubleQuotes() + " does not have " + value.ValueMember.AddDoubleQuotes() + " column");
                            }
                            if (value.DisplayMember.IsNull())
                            {
                                AddError("Browser " + value.Name.AddDoubleQuotes() + " does not have DisplayMember");
                            }
                            else if (!value.Columns.Contains(value.DisplayMember))
                            {
                                AddError("Browser " + value.Name.AddDoubleQuotes() + " does not have " + value.DisplayMember.AddDoubleQuotes() + " column");
                            }
                            if (!value.Static && value.Tables.IsNull())
                            {
                                AddError("Browser " + value.Name.AddDoubleQuotes() + " does not have any tables");
                            }
                            if (value.IsTreeBrowser)
                            {
                                if(value.IdColumn.IsNull() )
                                    AddError("Browser " + value.Name.AddDoubleQuotes() + " is TreeBrowser and does not have IdColumn");
                                else if(!value.Columns.Contains(value.IdColumn))
                                {
                                    AddError("Browser " + value.Name.AddDoubleQuotes() + " does not have " + value.IdColumn.AddDoubleQuotes() + " column");
                                }
                                if (value.ChildColumn.IsNull())
                                    AddError("Browser " + value.Name.AddDoubleQuotes() + " is TreeBrowser and does not have ChildColumn");
                                else if (!value.Columns.Contains(value.ChildColumn))
                                {
                                    AddError("Browser " + value.Name.AddDoubleQuotes() + " does not have " + value.ChildColumn.AddDoubleQuotes() + " column");
                                }
                            }
                            if (value.ConfigGroup.IsNull() && value.DisplayType == BrowserDisplayType.ConfigSpecified)
                            {
                                AddError("Browser " + value.Name.AddDoubleQuotes() + " needs ConfigGroup");
                            }
                        }
                    }
                }
            }
        }
    }
}
